import { defineStore } from 'pinia'
import { DEFAULT_ODDS, DEFAULT_REGION, DEFAULT_THEME } from '@/constants'
import { changeOdds, getCurrency, getCurrencySymbol, getLang, getUUID } from '@/utils'
import { AccountRegion, DeviceType, Lang, Odds, Region } from '@/enums'
import { getIpInfo } from '@/api/assistants'
import { getUnreadCount } from '@/api/notice'
import { AgentInfo } from '@/types'
import { useAccountStore, useProfileStore } from '@/store'
import { mobileAreaOptions } from './app-data'

export const useAppStore = defineStore('app', {
	state: () => ({
		lang: getLang(),
		theme: DEFAULT_THEME,
		odds: DEFAULT_ODDS as Odds,
		region: DEFAULT_REGION as Region,
		regionName: AccountRegion.un.name as any,
		uuid: '',
		version: '',
		agent: null as AgentInfo | null,
		ipInfo: null,
		settingShow: false,
		languageShow: false,
		mobileMenuShow: false,
		aboutShow: false,
		todoShow: false,
		lockShow: false,
		transactionFilter: '',
		casinoDetailShow: false,
		casinoDetailData: {
			game: {}
		},
		platform: DeviceType.pc,
		noticeUnread: 0,
		gaming: false,
		// 手机区号可能来自后台接口; regexp 使用 parseRegexp 方法解析
		mobileAreaOptions: [] as any[]
		// 国家列表数据量比较大, 列表过滤还需要翻译 有性能问题
		// countryOptions
	}),
	actions: {
		setCasinoDetailVisible(val: boolean, data: any) {
			this.casinoDetailShow = val
			if (val) {
				this.casinoDetailData = data
			}
		},
		setUUID() {
			this.uuid = getUUID()
		},
		setLang(val: Lang) {
			this.lang = val
		},
		setTheme(val: string) {
			this.theme = val
		},
		setOdds(val: Odds) {
			if (val === this.odds) return
			this.odds = val
			changeOdds(val)
		},
		setRegion(val: Region) {
			this.region = val
			this.regionName = Object.values(AccountRegion).find((item: any) => item.id === val)?.name
		},
		setAgent(val: AgentInfo) {
			this.agent = val
		},
		setGaming(val: boolean) {
			this.gaming = val
		},
		showSetting(val: boolean) {
			this.settingShow = val
		},
		showLanguage(val: boolean) {
			this.languageShow = val
		},
		showMobileMenu(val: boolean) {
			this.mobileMenuShow = val
		},
		showAbout(val: boolean) {
			const accountStore = useAccountStore()
			if (!accountStore.isLogin) {
				accountStore.showLogin()
				return
			}
			this.aboutShow = val
		},
		showTodo(val: boolean) {
			this.todoShow = val
		},
		showLock(val: boolean) {
			this.lockShow = val
		},
		setTransactionFilter(val: any) {
			this.transactionFilter = val
		},
		getMobileAreaOptions() {
			this.mobileAreaOptions = mobileAreaOptions
		},
		async getIpInfo() {
			const accountStore = useAccountStore()
			try {
				const { data } = await getIpInfo()
				this.ipInfo = data
				if (!accountStore.isLogin && data.location in Region) {
					this.region = data.location
				}
			} catch (error) {}
		},
		async getNoticeUnread() {
			try {
				const appStore = useAppStore()
				const profileStore = useProfileStore()
				const unreadParams = {
					currency: appStore.currency,
					platform: appStore.platform,
					locationId: appStore.region,
					locale: appStore.lang,
					origin: location.origin,
					vipLevel: profileStore.userInfo?.vip
				}
				const { data } = await getUnreadCount(unreadParams)
				this.noticeUnread = data.count
			} catch (error) {}
		},
		reduceUnreadCount() {
			if (this.noticeUnread) {
				this.noticeUnread = this.noticeUnread - 1
			}
		}
	},
	getters: {
		isUsdt: (state) => state.region === Region.un,
		currency: (state) => getCurrency(state.region),
		currencySymbol: (state) => getCurrencySymbol(state.region),
		allowable: (state) => {
			const email = useProfileStore().userInfo?.email
			const realName = useProfileStore().userInfo?.realName
			const birthday = useProfileStore().userInfo?.birthday
			const mobile = useProfileStore().userInfo?.mobile
			const isTure = Boolean(email && realName && birthday)
			return state.region === Region.un ? isTure : !!mobile && isTure
		}
	},
	persist: true
})
